package cn.wolfcode.wms.controller;

import cn.wolfcode.wms.qo.OrderChartQueryObject;
import cn.wolfcode.wms.qo.SaleChartQueryObject;
import cn.wolfcode.wms.service.IBrandService;
import cn.wolfcode.wms.service.ICharsService;
import cn.wolfcode.wms.service.IClientService;
import cn.wolfcode.wms.service.ISupplierService;
import cn.wolfcode.wms.util.RequiredPermission;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chart")
public class ChartsController {

    @Autowired
    private ICharsService charsService;
    @Autowired
    private IClientService clientService;
    @Autowired
    private IBrandService brandService;
    @Autowired
    private ISupplierService supplierService;


    @RequestMapping("/order")
    @RequiredPermission("订货报表")
    public String orderChart(@ModelAttribute("qo") OrderChartQueryObject qo, Map map) {
        map.put("groupTypes", OrderChartQueryObject.groupTypes);
        map.put("datas", charsService.orderChart(qo));
        map.put("suppliers", supplierService.listAll());
        map.put("brands", brandService.listAll());
        return "charts/order";
    }

    @RequestMapping("/sale")
    @RequiredPermission("销售报表")
    public String saleChart(@ModelAttribute("qo") SaleChartQueryObject qo, Map map) {
        map.put("groupTypes", SaleChartQueryObject.groupTypes);
        map.put("datas", charsService.saleChart(qo));
        map.put("clients", clientService.listAll());
        map.put("brands", brandService.listAll());
        return "charts/sale";
    }

    @RequestMapping("/saleChartByBar")
    @RequiredPermission("销售报表-柱状图")
    public String saleCharByBar(@ModelAttribute("qo") SaleChartQueryObject qo, Map map) {
        List<Map<String, Object>> maps = charsService.saleChart(qo);
        List<Object> groupTypes = new ArrayList<>();
        List<Object> totalAmounts = new ArrayList<>();
        //封装横纵坐标数据(分组类型和销售总额)
        for (Map<String, Object> objectMap : maps) {
            groupTypes.add(objectMap.get("groupType"));
            totalAmounts.add(objectMap.get("totalAmount"));
        }
        map.put("groupTypes", JSON.toJSONString(groupTypes));
        map.put("totalAmounts", totalAmounts);
        map.put("groupName", SaleChartQueryObject.groupTypes.get(qo.getGroupBy()));
        return "charts/saleChartByBar";
    }

    @RequestMapping("/saleChartByPie")
    @RequiredPermission("销售报表-饼状图")
    public String saleChartByPie(@ModelAttribute("qo") SaleChartQueryObject qo, Map map) {
        List<Map<String, Object>> maps = charsService.saleChart(qo);
        map.put("groupName", SaleChartQueryObject.groupTypes.get(qo.getGroupBy()));
        //图表横坐标数据
        List<Object> groupTypes = new ArrayList<>();
        //获取最大值
        BigDecimal maxAmount = BigDecimal.ZERO;
        //图表比例数据
        List<Object> datas = new ArrayList<>();
        for (Map<String, Object> objectMap : maps) {
            //比较最大值,然后交换
            BigDecimal amount = (BigDecimal) objectMap.get("totalAmount");
            if (amount.compareTo(maxAmount) >0){
                maxAmount = amount;
            }
            //封装横坐标数据
            groupTypes.add(objectMap.get("groupType"));
            //封装饼状图中单个元素数据
            Map<String, Object> data = new HashMap<>();
            data.put("name", objectMap.get("groupType"));
            data.put("value", amount);
            datas.add(data);

        }
        map.put("groupTypes", JSON.toJSONString(groupTypes));
        map.put("datas", JSON.toJSONString(datas));
        map.put("maxAmount", maxAmount);
        return "charts/saleChartByPie";
    }

}
